package com.imooc.demo.web;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.imooc.demo.entity.DeviceInfo;
import com.imooc.demo.service.DeviceInfosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/deviceinfo")
public class DeviceInfoController {
	@Autowired
	private DeviceInfosService deviceInfosService;

	/**
	 * 获取所有的区域信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/devicelist", method = RequestMethod.GET)
	private Map<String, Object> getDeviceInfos() {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		List<DeviceInfo> list = new ArrayList<>();
		// 获取区域列表
		list = deviceInfosService.getDeviceInfos();
		modelMap.put("devicelist", list);
		return modelMap;
	}

	/**
	 * 通过优先级Id获取区域信息
	 *
	 * @return
	 */
	@RequestMapping(value = "/getListByP", method = RequestMethod.GET)
	private Map<String, Object> getDeviceListByP(String propertyId) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		// 获取区域信息
		List<DeviceInfo> deviceInfos = deviceInfosService.queryDeviceInfoByP(propertyId);
		modelMap.put("deviceInfos", deviceInfos);
		return modelMap;
	}



	/**
	 * 通过优先级Id获取区域信息
	 *
	 * 	根据优先级 获取 任务列表
	 *
	 *  获取最近最少访问的设备信息
	 *
	 *  按照访问的时间倒叙排列 最近访问的排在最后
	 *
	 *  返回数量 暂定数组
	 *
	 * @return
	 */
	@RequestMapping(value = "/getListTopByP", method = RequestMethod.GET)
	private Map<String, Object> queryTopDeviceInfoByP(String priorityID) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		// 获取区域信息
		List<DeviceInfo> deviceInfos = deviceInfosService.queryTopDeviceInfoByP(priorityID);
		modelMap.put("deviceInfos", deviceInfos);
		return modelMap;
	}



	/**
	 * 添加区域信息
	 * 

	 * @return
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonParseException
	 */
	@RequestMapping(value = "/addDeviceInfo", method = RequestMethod.POST)
	private Map<String, Object> addDeviceInfo(@RequestBody DeviceInfo area)
			throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		// 添加区域信息
		modelMap.put("success", deviceInfosService.addDeviceInfo(area));
		return modelMap;
	}

	/**
	 * 修改区域信息，主要修改名字
	 * 

	 * @return
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonParseException
	 */
	@RequestMapping(value = "/updateDeviceInfo", method = RequestMethod.POST)
	private Map<String, Object> updateDeviceInfo(@RequestBody DeviceInfo area)
			throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		// 修改区域信息
		modelMap.put("success", deviceInfosService.updateDeviceInfo(area));
		return modelMap;
	}

	@RequestMapping(value = "/delDeviceInfo", method = RequestMethod.GET)
	private Map<String, Object> delDeviceInfo(String phoneNo ) {
		Map<String, Object> modelMap = new HashMap<String, Object>();

		modelMap.put("success", deviceInfosService.deleteDeviceInfo(phoneNo));
		return modelMap;
	}

}
